package cn.yueranzs.system.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.yueranzs.handler.BusinessException;
import cn.yueranzs.login.common.DataUtil;
import cn.yueranzs.response.Result;
import cn.yueranzs.response.ResultCode;
import cn.yueranzs.response.ResultPage;
import cn.yueranzs.system.dto.department.DeptChangeDto;
import cn.yueranzs.system.dto.department.DeptQueryDto;
import cn.yueranzs.system.pojo.Department;
import cn.yueranzs.system.service.DepartmentService;
import cn.yueranzs.utils.PaginationUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yueranzs
 * @since 2021-11-09
 */
@Api(tags = "部门控制器")
@RestController
@RequestMapping("/system/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询部门人数")
    @GetMapping("/findDeptList")
    public Result findDeptList(DeptQueryDto dto){
        Page page = PaginationUtil.getNewPage(dto.getCurrentPage(), dto.getPageSize());
        Department department = DataUtil.setData(dto,Department.class);
        ResultPage deptPage = departmentService.findDeptAndCount(department,page);
        return Result.successData(deptPage);
    }

    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "添加部门",notes = "添加部门")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(@RequestBody DeptChangeDto dto){
        try {
            Department department = DataUtil.setData(dto,Department.class);
            departmentService.saveDepartment(department);
            return Result.ok(ResultCode.SAVESUCCESS.getCode(),ResultCode.SAVESUCCESS.getMessage());
        }catch (BusinessException e){
            return Result.error(e.getCode(),e.getErrMsg());
        }
    }

    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "修改部门",notes = "修改部门")
    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody DeptChangeDto dto){
        if (ObjectUtil.isEmpty(dto.getId())) {
            return Result.error(ResultCode.DEPARTMENT_NOT_ID.getCode(),ResultCode.DEPARTMENT_NOT_ID.getMessage());
        }
        try {
            Department department = DataUtil.setData(dto,Department.class);
            departmentService.updateDepartment(department);
            return Result.ok(ResultCode.UPDATESUCCESS.getCode(),ResultCode.UPDATESUCCESS.getMessage());
        }catch (BusinessException e){
            return Result.error(e.getCode(),e.getErrMsg());
        }
    }

    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "导出部门",notes = "根据传入的参数导出部门列表")
    @PostMapping("/excelDeptList")
    public void excelDeptList(HttpServletResponse response, @RequestBody DeptQueryDto dto){
        Department department = DataUtil.setData(dto,Department.class);
        departmentService.excelDeptList(response,department);
    }

    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "删除部门",notes = "根据传入的参数删除部门")
    @PostMapping("/deleteDepartment/{id}")
    public Result deleteDepartment(@PathVariable("id") Long id){
        if (ObjectUtil.isEmpty(id)) {
            return Result.error(ResultCode.DEPARTMENT_NOT_ID.getCode(),ResultCode.DEPARTMENT_NOT_ID.getMessage());
        }
        try {
            departmentService.deleteDepartment(id);
            return Result.ok(ResultCode.DELETESUCCESS.getCode(),ResultCode.DELETESUCCESS.getMessage());
        }catch (BusinessException e){
            return Result.error(e.getCode(),e.getErrMsg());
        }
    }

}

